/**
 * 封装本地存储
 */
// 引入cookies的js库
import cookies from 'js-cookie'
//  转成字符串
const prefix = ''

export default {
    cookie: {
        get(key) {
            if (key) {
                //单个
                return cookies.get(prefix+key)
            } else {
                // 所有
                return cookies.get()
            }
        },
        getJson(key) {
          return cookies.getJson(prefix+key)  
        },
        set(key, value, path) {
            // 如果值设置了路径，需要在delete时指定路径
            if (path !== undefined) {
                cookies.set(prefix+key,value)
            } else {
                cookies.set(prefix+key,value,{path})
            }
        },
        remove(key, path) {
            if (path !== undefined) {
                cookies.remove(prefix+key)
            } else {
                cookies.remove(prefix+key,{path})
            }
        }
    },
    local: {
        set(key, value) {
            localStorage[prefix + key] = value;
        },
        setJson(key, value) {
            localStorage[prefix+key]=JSON.stringify(value)
        },
        get(key, defaultValue) {
            return localStorage[prefix+key]||defaultValue
        },
        getJson(key) {
            return JSON.parse(localStorage[prefix+key]||'{}')
        },
        remove(key) {
            localStorage.removeItem(prefix+key)
        },
        removeAll() {
          localStorage.clear()  
        }
    },
    session: {
        set(key, value) {
            sessionStorage[prefix + key] = value;
        },
        setJson(key, value) {
            sessionStorage[prefix+key]=JSON.stringify(value)
        },
        get(key, defaultValue) {
            return sessionStorage[prefix+key]||defaultValue
        },
        getJson(key) {
            return JSON.parse(sessionStorage[prefix+key]||'{}')
        },
        remove(key) {
            sessionStorage.removeItem(prefix+key)
        },
        removeAll() {
            sessionStorage.clear()  
        }
    },
}

